package sc;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class CallToArms extends JDialog implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private static CallToArms instance = null;
	private static JTextArea ta;
	private static long time = 0;
	private static PlayWav wav;
	private static Image win = Images.getImage("/images/harlequin.png");
	private static Image icon = Images.getImage("/images/calltoarms.png");
	
	class ImagePanel extends JPanel
	{
		private static final long serialVersionUID = 1L;

		public ImagePanel()
		{
			Dimension d = new Dimension(icon.getWidth(this) + 20, icon.getHeight(this) + 20);
			setPreferredSize(d);
		}
		
		@Override
		protected void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			g.drawImage(icon, 10, 10, this);
		}
	}
	
	private CallToArms()
	{
		setIconImage(win);
		setTitle("Harlequin: Call To Arms");
		
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		JPanel subpanel = new JPanel();
		subpanel.add(new ImagePanel());
		ta = new JTextArea();
		ta.setEditable(false);
		ta.setBackground(getBackground());
		subpanel.add(ta);
		c.add(subpanel, BorderLayout.CENTER);
		
		subpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton button = new JButton("  Ok  ");
		button.addActionListener(this);
		subpanel.add(button);
		
		c.add(subpanel, BorderLayout.SOUTH);
	}

	public static void open(String message)
	{
		// Sanitise the message
		if (message == null || message.length() < 5)
			message = "No information given";
		else
			message = message.substring(4);
		
		long now = System.nanoTime();
		
		// If called again within 1 minute of the previous call do nothing
		if (CallToArms.time != 0 && CallToArms.time > now - 60000000000l)
			return;
		
		if (CallToArms.instance == null)
			CallToArms.instance = new CallToArms();
		
		CallToArms c = CallToArms.instance;
		String sanitised = "";
		int linewidth = 0;
		
		for (int i = 0; i < message.length(); i++)
		{
			linewidth++;
			if (message.charAt(i) == ' ' && linewidth > 20)
			{
				sanitised += '\n';
				linewidth = 0;
			}
			else
				sanitised += message.charAt(i);
		}
		CallToArms.ta.setText(sanitised);
		c.setResizable(true);
		c.pack();
		c.setResizable(false);
		CallToArms.time = now;
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		c.setLocation((int)((dim.getWidth() / 2f)- (c.getWidth() / 2f)), (int)((dim.getHeight() / 2f) - (c.getHeight() / 2f)));
		c.setVisible(true);

		c.setAlwaysOnTop(true);
		c.setAlwaysOnTop(false);
		
		CallToArms.wav = new PlayWav("/sounds/calltoarms.wav");
		CallToArms.wav.start();

		Tray.event("Call to Arms: " + message);
	}

	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		if (cmd == "  Ok  ")
		{
			CallToArms.wav.end();
			setVisible(false);
		}
	}
}
